/*
 * Copyright (C) 2010-2011 Graz University of Technology, Austria. All Rights reserved. 
 *
 * Contact: Simone Kopeinik <simone.kopeinik@tugraz.at>
 * 	   Graz University of Technology, Knowledge Management Institute, Brückenkopfgasse 1/6, 8020 Graz 	
 * 	   <http://www.kmi.tugraz.at/>
 * 
 * This software is part of the TARGET platform developed by the
 * TARGET EC-funded project www.reachyourtarget.org
 *
 * This software is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, version 3.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * The GNU General Public License is available at <http://www.gnu.org/licenses/>.
 *
 */
package at.tugraz.kmi.css.cbkst2;

import com.google.common.collect.HashMultimap;
import com.google.common.collect.ImmutableCollection;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Multimap;
import com.google.common.collect.Multimaps;

/**
 * Also known as "Skill Function". Implements a mapping between performance and
 * competence.
 * 
 * @author georgo
 * 
 */
class CompetenceFunction implements
		TransferFunction<CbKSTCompetence, CbKSTPerformance> {

	final Multimap<CbKSTCompetence, CbKSTPerformance>	skillMapping;

	/**
	 * Default Constructor.
	 */
	public CompetenceFunction() {
		this(null);
	}

	CompetenceFunction(Multimap<CbKSTCompetence, CbKSTPerformance> graphMapping) {
		if (graphMapping == null) {
			skillMapping = HashMultimap.create();
		} else {
			skillMapping = graphMapping;
		}
	}

	/**
	 * Adds {@link CbKSTCompetence} to {@link CbKSTPerformance} mappings.
	 * 
	 * @param competence
	 * @param problem
	 */
	@Override
	public void addMapping(CbKSTCompetence competence,
			CbKSTPerformance... problem) {
		skillMapping.putAll(competence, ImmutableSet.copyOf(problem));
	}

	/**
	 * 
	 * The {@code PerformanceFunction} function is the inverse of the
	 * {@code CompetenceFunction}.
	 * 
	 * @return A copy of this {@code PerformanceFunction} as
	 *         {@code CompetenceFunction}.
	 */
	@Override
	public PerformanceFunction invert() {
		Multimap<CbKSTPerformance, CbKSTCompetence> problemMapping = HashMultimap
				.create();
		Multimaps.invertFrom(skillMapping, problemMapping);
		return new PerformanceFunction(problemMapping);
	}

	@Override
	public ImmutableCollection<CbKSTPerformance> get(CbKSTCompetence key) {
		return ImmutableList.copyOf(skillMapping.get(key));
	}
}
